package com.chukun.flink.stream.window.process;

import com.chukun.flink.stream.window.process.function.ExportProcessWindow;
import com.chukun.flink.stream.window.source.SourceForWindow;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * @author chukun
 * @version 1.0.0
 * @description 窗口 process函数与reduce函数操作
 * @createTime 2022年05月22日 23:38:00
 */
public class ProcessWindowReduceOperator {

    public static void main(String[] args) throws Exception {

        // 创建运行环境
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 设置作业的全局并行度 1
        env.setParallelism(1);

        // 添加数据源
        DataStreamSource<Tuple3<String, Integer, String>> stremSource = env.addSource(new SourceForWindow(1000, false));

        // 根据数据流中的元素f0字段作为作为key对数据流分组
        DataStream<Tuple2<Long, Tuple3<String, Integer, String>>> reduceStream = stremSource.keyBy((key) -> key.f0)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                // 对窗口应用reduce + process窗口函数, ReduceFunction用于增量汇总到达的数据, ExportProcessWindow用于提供聚合结果
                .reduce(new ReduceFunction<Tuple3<String, Integer, String>>() {
                    @Override
                    public Tuple3<String, Integer, String> reduce(Tuple3<String, Integer, String> value01, Tuple3<String, Integer, String> value02) throws Exception {
                        return value01.f1 > value02.f1 ? value01 : value02;
                    }
                }, new ExportProcessWindow());

        reduceStream.print("窗口-reduce-process计算: ");

        env.execute("ProcessWindowReduceOperator");
    }
}
